package com.genius.smsweb.admin;

import com.alibaba.fastjson.JSONObject;
import com.genius.entity.LoginBean;
import com.genius.entity.UserBean;
import com.genius.smsbase.Controller.GenericController;
import com.genius.smsbase.Domain.Result;
import com.genius.smsbase.util.ResultUtil;
import com.genius.smsdao.admin.domain.User;
import com.genius.smsdao.studentmanage.domain.Student;
import com.genius.smsservice.admin.service.UserManager;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import io.swagger.annotations.Api;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;

@Api(tags = "用户管理", description = "UserController")
@RestController
@RequestMapping(value = "user")
public class UserController extends GenericController<User,Long,UserManager> {

    UserManager userManager;

    @Resource
    private Producer producer;

    @Autowired
    public void setUserManager(UserManager userManager){
        this.userManager = userManager;
        this.manager = userManager;
    }


    /**
     * 验证码接口
     *
     * @param response
     * @param request
     * @throws ServletException
     * @throws IOException
     */
    @GetMapping(value = "captcha.jpg")
    public void captcha(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        // 生成文字验证码
        String text = producer.createText();
        // 生成图片验证码
        BufferedImage image = producer.createImage(text);
        // 保存到验证码到 session
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_DATE, text);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }

    /**
     *
     * @param loginBean
     * @param request
     * @return Result
     */
    @PostMapping("login")
    public Result login(@RequestBody LoginBean loginBean, HttpServletRequest request){
        String username = loginBean.getAccount();
        String password = loginBean.getPassword();
        String captcha = loginBean.getCaptcha();
        // 从session中获取之前保存的验证码跟前台传来的验证码进行匹配
        Object kaptcha = request.getSession().getAttribute(Constants.KAPTCHA_SESSION_DATE);
        if (kaptcha == null) {
            return ResultUtil.error("验证码已失效");
        }
        if (!captcha.equals(kaptcha)) {
            return ResultUtil.error("验证码不正确");
        }
        // 用户信息
        User user = userManager.findByUsername(username);
        // 账号不存在、密码错误
        if (user == null) {
            return ResultUtil.error("用户名或密码错误");
        }
        if (!password.equals(user.getPassword())) {
            return ResultUtil.error("用户名或密码错误");
        }
        return ResultUtil.success("登录成功");

    }
    /**
     * updatepswd修改密码
     * @return Result
     */
    @RequestMapping(value = "updatePassword",method = RequestMethod.PUT)
    @CrossOrigin
    public Result updatePassword(@RequestBody UserBean userBean, HttpSession session){


        String oldPassword = userBean.getOldPassword();
        String newPassword = userBean.getNewPassword();
        String repeatPassword = userBean.getRepeatPassword();

        User user = userManager.findUserByPassword(oldPassword);
//        if (oldPassword==null || newPassword==null || repeatPassword==null) {
//         return  ResultUtil.error("密码不能为空");
//        }else
            if(!oldPassword.equals(user.getPassword())){
            return ResultUtil.error("您输入的旧密码错误，请重新输入");
        }
        else{
            user.setPassword(newPassword);
            userManager.save(user);
            return ResultUtil.success("密码修改成功");

        }

    }

}
